package felix.things.router.mqtt.credentials;

import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author Felix
 * @date 2019/7/27
 */
@Data
@Component
@ConditionalOnProperty(prefix = "mqttrouter.config.credentials", value = "type", havingValue = "basic")
@ConfigurationProperties(prefix = "mqttrouter.config.credentials", ignoreInvalidFields = true)
public class BasicCredentials implements MqttClientCredentials {

    private String username;
    private String password;

    @Override
    public void configure(MqttConnectOptions clientOptions) {
        clientOptions.setUserName(username);
        if (!StringUtils.isEmpty(password)) {
            clientOptions.setPassword(password.toCharArray());
        }
    }
}
